var selectedRow = null;
var selectedAppRow = null;
var isDataInit = true,isCheck=true;

// 默认选中事件
var refresh =0;
var isInit = true;

$(function () {
    regValidate(["#_bizDataForm"]);
    window.actionEvents = {
        'click .infoView': function(e, value, row, index) {
            //编辑操作
            selectedRow = row;
            openEnumPage();
        },
    }
    newCheck.initByName('del_text');
});

function doRefresh() {
    selectedRow = null;
    var bizName = $.trim($("#bizName").val());
    var status = $.trim($("#Pstatus").val());
    $('#dataTable').bootstrapTable('refresh', {
        url: basePath + 'bizApp/getGridBizAppData?t=' + new Date().getTime(),
        query: {
            bizName : bizName,
            status : status,
            // isvPid : selectedAppRow ? selectedAppRow.isvPid : '',
            // refCode : selectedAppRow ? selectedAppRow.refCode : '',
            // bizParentCode : selectedAppRow ? selectedAppRow.bizCode : '',
            isvPid : $.trim($("#pIsvPid").val()),
            refCode : $.trim($("#pRefCode").val()),
            bizParentCode : $.trim($("#pBizCode").val()),
            appCategory : 'org',
            isParent : '0',
        },
    });
}

function getParams(params) {
    var postData = {
        appCategory : 'org',
        isParent : '0',
        pageSize : params.pageSize,
        pageNo : params.pageNumber
    };
    postData.bizName = $.trim($("#bizName").val());
    postData.status = $.trim($("#Pstatus").val());
    if (isDataInit) {
        isDataInit = false;
        //     isvPid : $.trim($("#pAppId").val()),
        //     refCode : $.trim($("#pRefCode").val()),
        //     bizParentCode : $.trim($("#pBizCode").val()),
        postData.isvPid = $.trim($("#pIsvPid").val());
        postData.refCode = $.trim($("#pRefCode").val());
        postData.bizParentCode =$.trim($("#pBizCode").val());
    } else if(selectedAppRow) {
        // postData.isvPid = selectedAppRow.isvPid;
        // postData.refCode = selectedAppRow.refCode;
        // postData.bizParentCode = selectedAppRow.bizCode;
        postData.isvPid = $.trim($("#pIsvPid").val());
        postData.refCode = $.trim($("#pRefCode").val());
        postData.bizParentCode =$.trim($("#pBizCode").val());
    }
    return postData;
}


function doAppRefresh() {
    $('#dataAppTable').bootstrapTable('refresh', {
        url: basePath + 'bizApp/getGridBizAppData?t=' + new Date().getTime(),
    });
}

function getAppParams(params) {
    var  postData = {
        appCategory : 'org',
        isParent : '1',
        pageSize : 100000,
        pageNo : 1
    };
    postData[$("#search").attr("name")] = $("#search").val();
    return postData;
}


function getWinParams(params) {
    var  postData = {
        appCategory : 'org',
        pageSize : params.pageSize,
        pageNo : params.pageNumber
    };
    // postData.isvPid = selectedAppRow.isvPid;
    // postData.refCode = selectedAppRow.refCode;
    // postData.bizCode = selectedAppRow.bizCode;
    postData.isvPid  = $.trim($("#pIsvPid").val());
    postData.refCode = $.trim($("#pRefCode").val());
    postData.bizCode = $.trim($("#pBizCode").val());
    postData.bizName = $.trim($("#winBizName").val());
    return postData;
}
function doWinRefresh() {
    $('#WinTable').bootstrapTable('refresh', {
        url: basePath + 'bizApp/getGridBizDataChlid?t=' + new Date().getTime(),
    });
}

$("#dataAppTable").on("click-row.bs.table",function(e,row,ele){
    $("#dataAppTable .success").removeClass("success");
    $(ele).addClass("success");
    selectedAppRow = row;
    // postData.isvPid  = $.trim($("#pAppId").val());
    // postData.refCode = $.trim($("#pRefCode").val());
    // postData.bizCode = $.trim($("#pBizCode").val());
    // postData.bizName = $.trim($("#winBizName").val());
    $("#pAppId").val(row.appid);
    $("#pIsvPid").val(row.isvPid);
    $("#pRefCode").val(row.refCode);
    $("#pBizCode").val(row.bizCode);
    $("#pIsvName").val(row.isvName);
    $("#pOrgName").val(row.orgName);
    $("#pBizName").val(row.bizName);

    // 这是一个编辑为确定是否点击
    isCheck = false;
    doRefresh();
});


var selcetIsv = null;

function formatStatus(value, row, index) {
    if (value == 'enabled') {
        return '有效';
    }
    if (value == 'disabled') {
        return '无效';
    }
    return value;
}

//右边选中事件
$("#dataTable").on("click-row.bs.table",function(e,row,ele){
    $("#dataTable .success").removeClass("success");
    $(ele).addClass("success");
    selectedRow = row;
});

function doSaveApp() {
    if(selectedAppRow == null){
        toastr.error("请选择服务机构");
        return;
    }
    var selectRows = getSelectedRows("#WinTable");
    if(selectRows == null || selectRows.length == 0){
        toastr.error("请选择服务项");
        return;
    }
    var list = new Array();
    for (var index in selectRows){
        list.push({
            // selectedAppRow.refCode,
            refCode:$.trim($("#pRefCode").val()),
            bizCode:selectRows[index].bizCode,
            appName:selectRows[index].bizName,
            appDisplayName:selectRows[index].bizName,
            appType:selectRows[index].otherServiceType,
            hasMidPage:selectRows[index].hasMidPage,
            status:'publish'
        });
    }

    $.ajax({
        type: 'post',
        url: basePath + 'bizApp/addBizAppChlid',
        dataType: 'json',
        contentType: "application/json",
        beforeSend:function () {
            showLoading();
        },
        complete:function(){
            hideLoading();
        },
        data: JSON.stringify({isvPid:selectedAppRow.isvPid,appid:selectedAppRow.appid,refDataList:list,appCategory:'org'}),
        success : function (data) {
            if(data.result){
                toastr.success(common_save_success);
                doRefresh();
                $("#addBizApp").modal('hide');
            }else{
                toastr.error(data.message);
            }
        },
        error: function (request) {
            toastr.error(common_system_error);
        }
    });

}

/******************************************************************************************/
var VueObj = new Vue({
    el:"#VueObj",
    data:{addBizApp:{},editBizApp:[]}
});
function add() {
    if($.trim($("#pAppId").val()).length == 0){
        toastr.error(common_no_choose_msg.format("操作"));
        return;
    }
    $("#winBizName").val("");
    var selectValue = {};
    selectValue.bizName = $.trim($("#pBizName").val());
    selectValue.orgName = $.trim($("#pOrgName").val());
    selectValue.isvName = $.trim($("#pIsvName").val());
    VueObj.$set(VueObj.$data,"addBizApp",JSON.parse(JSON.stringify(selectValue)));
    doWinRefresh();
   $("#addBizApp").modal(true);
};

function edit() {
    if(selectedRow == null){
       toastr.error(common_no_choose_msg.format("操作"));
       return;
    }
    onOpenEditEvent('edit', selectedRow.id, selectedRow.appid, selectedRow.bizCode);
    /*VueObj.$set(VueObj.$data,"addBizApp",JSON.parse(JSON.stringify(selectedAppRow)));
    VueObj.$set(VueObj.$data,"editBizApp",JSON.parse(JSON.stringify(selectedRow)));
    ue.setContent(selectedRow.serviceContent);
    $("#editBizApp").modal(true);*/
}

function onOpenEditEvent(command, id, appid, bizCode) {
    var tableOptions1 = $('#dataAppTable').bootstrapTable('getOptions', null);
    var tableOptions2 = $('#dataTable').bootstrapTable('getOptions', null);
    var cBizName = $('#bizName').val();
    var cStatus = $('#Pstatus').val();
    var cOrgName = $('#search').val();
    // var pAppId = selectedAppRow ? selectedAppRow.appid : '';
    // var pIsvPid = selectedAppRow ? selectedAppRow.isvPid : '';
    // var pRefCode = selectedAppRow ? selectedAppRow.refCode : '';
    // var pBizCode = selectedAppRow ? selectedAppRow.bizCode : '';
    var pAppId = $.trim($("#pAppId").val());
    var pIsvPid = $.trim($("#pIsvPid").val());
    var pRefCode = $.trim($("#pRefCode").val());
    var pBizCode = $.trim($("#pBizCode").val());
    // 新增时候的冗余字段
    var pIsvName = $.trim($("#pIsvName").val());
    var pOrgName = $.trim($("#pOrgName").val());
    var pBizName = $.trim($("#pBizName").val());
    window.location.href = basePath + 'bizApp/editChildOrgBizAppPage?editType=' + command +
        '&id=' + id +
        '&appid=' + appid +
        '&sBizCode=' + bizCode +
        '&t=' + new Date().getTime() +
        '&pAppId=' + pAppId +
        '&pIsvPid=' + pIsvPid +
        '&pRefCode=' + pRefCode +
        '&pBizCode=' + pBizCode +
        '&pIsvName=' + pIsvName +
        '&pOrgName=' + pOrgName +
        '&pBizName=' + pBizName +
        '&cBizName=' + cBizName +
        '&cStatus=' + cStatus +
        '&cOrgName=' + cOrgName +
        '&cPageNo1=' + tableOptions1.pageNumber +
        '&cPageSize1=' + tableOptions1.pageSize +
        '&cPageNo2=' + tableOptions2.pageNumber +
        '&cPageSize2=' + tableOptions2.pageSize +
        '&isTeamExist=' + searchInfo.isTeamExist +
        '&isDocExist=' + searchInfo.isDocExist +
        '&orgName=' + searchInfo.orgName +
        '&bizCode=' + searchInfo.bizCode +
        '&status=' + searchInfo.status +
        '&dataStatus=' + searchInfo.dataStatus +
        '&isvPid=' + searchInfo.isvPid +
        '&isvName=' + searchInfo.isvName +
        '&searchStr=' + searchStr
    ;
}

function validateBizApp() {
    var postData = {};
    postData.appid = selectedRow.appid;
    postData.refCode = selectedRow.refCode;
    postData.bizCode = selectedRow.bizCode;
    postData.appCategory = selectedRow.appCategory;
    $.ajax({
        type: 'post',
        url: basePath + 'bizApp/validateBizAppChild',
        dataType: 'json',
        contentType: "application/json",
        data: JSON.stringify(postData),
        success : function (data) {
            if(data.result){
                dodel();
            }else{
                toastr.error(data.message);
            }
        },
        error: function (request) {
            toastr.error(common_system_error);
        }
    });
}



function dodel() {
    if(selectedRow == null){
        toastr.error(common_no_choose_msg.format("操作"));
        return;
    }
    $("input[name=del_text]").removeAttrs('checked');
    $("#del_textarea").val("");
    // if(selectedRow.dataStatus == 'pending_check'){
    //     toastr.error("这是待审核数据不能删除");
    //     return;
    // }
    // if(selectedRow.dataStatus == 'check_pass'){
    //     toastr.error("这是审核通过的数据请,先撤销再删除");
    //     return;
    // }
    $("#delAppWin").modal(true);
}


function dodelApp() {
    var result = new Array();
    $("input[name=del_text]:checked").each(function (index,value) {
        result.push($(value).val());
    });
    if(result.length==0 && $("#del_textarea").val() == ''){
        toastr.error("请选择撤销原因");
        return;
    }
    result.push($("#del_textarea").val());
    var postData = {};
    postData.id = selectedRow.id;
    postData.appid = selectedRow.appid;
    postData.isvPid = selectedRow.isvPid;
    postData.refCode = selectedRow.refCode;
    postData.bizCode = selectedRow.bizCode;
    postData.appCategory = selectedRow.appCategory;
    postData.status = selectedRow.status;
    postData.dataStatus = 'delete';
    postData.remark = result.join(",");
    $.ajax({
        type: 'post',
        url: basePath + 'bizApp/delBizAppChild',
        dataType: 'json',
        contentType: "application/json",
        beforeSend:function () {
            showLoading();
        },
        complete:function(){
            hideLoading();
        },
        data: JSON.stringify(postData),
        success : function (data) {
            if(data.result){
                toastr.success(data.message);
                doRefresh();
                $("#delAppWin").modal('hide');
            }else{
                toastr.error(data.message);
            }
        },
        error: function (request) {
            toastr.error(common_system_error);
        }
    });
}


function doback() {
    var str = searchStr ? searchStr.replace(/,/g,"&") : '';
    window.location.href = basePath+"bizApp/orgPage?t="+new Date().getTime()+ str
        '&isTeamExist=' + searchInfo.isTeamExist +
        '&isDocExist=' + searchInfo.isDocExist;
}


/***************************************************************************************/
function selectType() {
  var selectType =  $("#selectType").val();
  switch (selectType){
      case 'orgName':
      {
          $("#search").attr("name",selectType);
          $("#search").attr("placeholder","输入机构名称");
          break;
      };
      case 'bizName':
      {
          $("#search").attr("name",selectType);
          $("#search").attr("placeholder","输入业务类型");
          break;
      };
      case 'isvName':
      {
          $("#search").attr("name",selectType);
          $("#search").attr("placeholder","输入服务商名称");
          break;
      };
  }
}

function formatterStatus(value,row,index) {
    if(statusEnum != null){
        return statusEnum[value];
    }
    return "";
};


function onchangeh5(e) {
    if($(e).val() == 'html'){
        $("#h5").removeAttrs("hidden");
    }else{
        $("#h5").attr("hidden",'hidden');
    }
}

function onceCheck(value, row, index) {

    if (row.appid == $.trim($("#pAppId").val()) && isCheck) {
        setTimeout(function(){
            $("#dataAppTable > tbody > tr[data-index='" +index+"']").addClass("success");
        },200);
        // // 确认已经
        // isInit = false;
        selectedAppRow = row;
    }
    return value;
}